package com.mairuide.transaction;

import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide.bill.DealFlow;
import com.mairuide.bill.utils.DateUtils;
import com.mairuide.bill.utils.ExcelUtils;
import com.mairuide.datalog.ActionType;
import com.mairuide.datalog.DataOperationLogService;
import com.mairuide.datalog.ModuleFlag;
import com.mairuide.datalog.SystemFlag;
import com.mairuide.entity.PageResult;
import com.mairuide.transactionstreamcount.DealflowVO;

@Controller
@CrossOrigin
@RequestMapping("/bill/stream")
public class BankStatisticsController extends BaseController {

    @Autowired
    private BankStatisticsService bankStatisticsService;
    @Resource
    private DataOperationLogService dataOperationLogService;

    @ModuleCode(pValue = "operations", value = "Robot")
    @RequestMapping(value = "/capacity",method = RequestMethod.GET)
    public Object xxxxx(ModelMap model){
        return "/account/capacity";
    }

    @ModuleCode(pValue = "billstatus", value = "billflow")
    @RequestMapping(value = "/statistics",method = RequestMethod.GET)
    public Object transactionflowstatistics(ModelMap model){
        return "/stream/transactionfowstatistics";
    }
    @RequestMapping(value = "/formsubmission",method = RequestMethod.GET)
    public Object formsubmt(ModelMap model){
        return "/account/formsubmission";
    }

    @ModuleCode(pValue = "billstatus", value = "billskill")
    @RequestMapping(value = "/commonanalysis",method = RequestMethod.GET)
    public Object commonanalytical(ModelMap model){
        return "/methodsanalysis/commonanalysis";
    }

    @RequestMapping(value = "/inaccountStatisticsDialogInit", method = RequestMethod.GET)
    public Object inaccountStatisticsDialogInit(ModelMap model) {
        return "/bill/billDetailDialog";
    }

    //获取交易银行详情
    @RequestMapping(value = "/bank/content",method = RequestMethod.POST)
    @ResponseBody
    public Object outAccountStatistics(ModelMap model,String sort,String order,int page, int rows,
                                       @RequestParam(required = false) String rivalName, String rivalCard,String keyword,
                                       @RequestParam(required = false,value = "endTime")String endTime,
                                       @RequestParam(required = false,value = "startTime")String startTime,String bodyCards,String caseId){
        return bankStatisticsService.getBankStatisticsContentList(bodyCards , rivalCard, caseId, rivalName, startTime, endTime,sort,order,page,rows,keyword);
    }

    //获取交易详情
    @ResponseBody
    @RequestMapping(value = "/bank/body",method = RequestMethod.POST)
    public Object bodyDetail(DealflowVO dealflowVO){
        return bankStatisticsService.getBodyDetail(dealflowVO);
    }

    //导出Excel
    @RequestMapping("/exportDetail")
    public String createExcel(HttpServletResponse response, DealflowVO dealflowVO) throws Exception {

        //创建HSSFWorkbook对象(excel的文档对象)
        XSSFWorkbook workbook1 = new XSSFWorkbook();
        SXSSFWorkbook wb = new SXSSFWorkbook(workbook1, 100);
        CellStyle cellStyle = wb.createCellStyle();
        //设置居中:
        //设置字体:
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);//设置字体大小
        cellStyle.setFont(font);//选择需要用到的字体格式
        //建立新的sheet对象（excel的表单）
        Sheet sheet=wb.createSheet("账单详情表");
        //在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
        Row row1=sheet.createRow(0);
        //创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        Cell cell=row1.createCell(0);
        //设置单元格内容
        cell.setCellValue("账单详情");
        //合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,22));

        //在sheet里创建第二行
        Row row2=sheet.createRow(1);
        //创建单元格并设置单元格内容
        row2.createCell(0).setCellValue("交易主体");
        row2.createCell(1).setCellValue("交易主体账号");
        row2.createCell(2).setCellValue("交易主体卡号");
        row2.createCell(3).setCellValue("收付标志");
        row2.createCell(4).setCellValue("交易日期");
        row2.createCell(5).setCellValue("交易对手");
        row2.createCell(6).setCellValue("交易对手账号");
        row2.createCell(7).setCellValue("交易对手卡号");
        row2.createCell(8).setCellValue("交易金额");
        row2.createCell(9).setCellValue("交易后余额");
        row2.createCell(10).setCellValue("摘要");
        row2.createCell(11).setCellValue("交易主体归属行");
        row2.createCell(12).setCellValue("交易对手归属行");
        row2.createCell(13).setCellValue("交易地点");
        row2.createCell(14).setCellValue("交易方式");
        row2.createCell(15).setCellValue("交易流水号");
        row2.createCell(16).setCellValue("MAC地址");
        row2.createCell(17).setCellValue("IP地址");
        row2.createCell(18).setCellValue("币种");
        row2.createCell(19).setCellValue("备注");
        row2.createCell(20).setCellValue("交易机构号");
        row2.createCell(21).setCellValue("柜员号");
        row2.createCell(22).setCellValue("对方机构号");
        //在sheet里创建第三行
        dealflowVO.setPageNumber(0);
        dealflowVO.setPageSize(10);
        PageResult bodyDetail = bankStatisticsService.getBodyDetail(dealflowVO);
        List<Map<String,Object>> recordList = (List<Map<String,Object>>)bodyDetail.getRows();
        if (recordList!=null && recordList.size()>0){
            for (int i=0;i<recordList.size();i++){
                Map<String,Object> dealFlow = recordList.get(i);
                Row row3=sheet.createRow(i+2);
                // 交易主体,
                cell = row3.createCell(0);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_BODY"));
                // 交易主体账号,
                cell = row3.createCell(1);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_BODY_NUMBER"));
                // 交易主体卡号,
                cell = row3.createCell(2);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_BODY_CARD"));
                // 收付标志,
                cell = row3.createCell(3);
                cell.setCellValue(Integer.parseInt(dealFlow.get("SIGN").toString())==0?"进":"出");
                // 交易日期,
                cell = row3.createCell(4);
                cell.setCellValue(dealFlow.get("TRADE_DATE")==null?"":DateUtils.getDateString((Timestamp)dealFlow.get("TRADE_DATE")));
                // 交易对手,
                cell = row3.createCell(5);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_RIVAL"));
                // 交易对手账号,
                cell = row3.createCell(6);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_RIVAL_NUMBER"));
                // 交易对手卡号,
                cell = row3.createCell(7);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_RIVAL_CARD"));
                // 交易金额,
                cell = row3.createCell(8);
                cell.setCellValue(dealFlow.get("ACCOUNT_MONEY")==null?"":String.valueOf(dealFlow.get("ACCOUNT_MONEY")));
                // 交易后余额,
                cell = row3.createCell(9);
                cell.setCellValue(dealFlow.get("SURPLUS_MONEY")==null?"":String.valueOf(dealFlow.get("SURPLUS_MONEY")));
                // 摘要,
                cell = row3.createCell(10);
                cell.setCellValue((String)dealFlow.get("CONTENT_ABSTRACT"));
                // 交易主体归属行,
                cell = row3.createCell(11);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_BODY_BANK"));
                // 交易对手归属行,
                cell = row3.createCell(12);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_RIVAL_BANK"));
                // 交易地点,
                cell = row3.createCell(13);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_ADDRESS"));
                // 交易方式,
                cell = row3.createCell(14);
                cell.setCellValue((String)dealFlow.get("ACCOUNT_MODE"));
                // 交易流水,
                cell = row3.createCell(15);
                cell.setCellValue((String)dealFlow.get("SERIAL_NUMBER"));
                // mac,
                cell = row3.createCell(16);
                cell.setCellValue((String)dealFlow.get("MAC_ADDRESS"));
                // ip,
                cell = row3.createCell(17);
                cell.setCellValue((String)dealFlow.get("IP_ADDRESS"));
                // 币种,
                cell = row3.createCell(18);
                cell.setCellValue((String)dealFlow.get("CURRENCY"));
                // 备注,
                cell = row3.createCell(19);
                cell.setCellValue((String)dealFlow.get("REMARKS"));
                // 交易机构号,
                cell = row3.createCell(20);
                cell.setCellValue((String)dealFlow.get("TRADING_ORGANIZATION_NUMBER"));
                // 柜员号,
                cell = row3.createCell(21);
                cell.setCellValue((String)dealFlow.get("TELLER_NUMBER"));
                // 对方机构号,
                cell = row3.createCell(22);
                cell.setCellValue((String)dealFlow.get("INSTITUTION_NUMBER"));
            }
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileName = "账单信息" + dateFormat.format(new Date());
        //输出Excel文件
        OutputStream output=response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename="+ ExcelUtils.toUtf8String(fileName+".xlsx"));
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
        //添加导出日志
        dataOperationLogService.save(null, super.getLoginUser(), new DealFlow(), new DealFlow(), SystemFlag.ZD, ModuleFlag.BILLEXPORT, ActionType.EXPORT);
        return null;
    }

}
